package com.bzxgc.sgwer.controller.role;

import com.bzxgc.sgwer.common.page.Constant;
import com.bzxgc.sgwer.common.util.JsonResult;
import com.bzxgc.sgwer.common.util.PageData;
import com.bzxgc.sgwer.entity.role.Role;
import com.bzxgc.sgwer.entity.role.RoleMenu;
import com.bzxgc.sgwer.service.role.RoleMenuService;
import com.bzxgc.sgwer.service.role.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private RoleMenuService roleMenuService;

    @RequestMapping("/add")
    public String add() {
        return "role/save";
    }

    @RequestMapping("/update/{id}")
    public String update(@PathVariable("id") Long id, HttpServletRequest request) {
        Role role = roleService.getRoleById(id);
        request.setAttribute("role", role);
        return "role/save";
    }

    @RequestMapping("/ajax/save")
    @ResponseBody
    public JsonResult ajaxSave(Role role) {
        if (role.getId() == null) {
            roleService.addRole(role);
        } else {
            roleService.updateRole(role);
        }
        return JsonResult.success("保存成功");
    }

    @RequestMapping("/ajax/delete")
    @ResponseBody
    public JsonResult ajaxDelete(@RequestParam("ids") Long[] ids) {
        roleService.updateRoleStatus(ids, 2);
        return JsonResult.success("删除成功");
    }

    @RequestMapping("/list")
    public String list(HttpServletRequest request) {
        int pageNum = Constant.DEFAULT_PAGE_NUM;
        String pageNumStr = request.getParameter("pageNum");
        if (pageNumStr != null && !pageNumStr.equals("")) {
            pageNum = Integer.parseInt(pageNumStr); // 显示第几页数据
        }

        int pageSize = Constant.DEFAULT_PAGE_SIZE;
        String pageSizeStr = request.getParameter("pageSize");
        if (pageSizeStr != null && !pageSizeStr.equals("")) {
            pageSize = Integer.parseInt(pageSizeStr); // 每页显示多少条记录
        }

        String name = request.getParameter("name");
        PageData<Role> pageData = roleService.getRoleListByPage(pageNum, pageSize, name);
        request.setAttribute("pageData", pageData);
        request.setAttribute("name", name);
        request.setAttribute("pageNum", pageNum);
        return "role/list";
    }

    @RequestMapping("/menu/setting")
    public String menuSetting() {
        return "role/menu-setting";
    }

    @RequestMapping("/ajax/save/setting")
    @ResponseBody
    public JsonResult ajaxMenuSetting(@RequestParam("roleIds") Long[] roleIds, @RequestParam("menuIds") Long[] menuIds) {
        roleMenuService.addRoleMenuBatch(roleIds, menuIds);
        return JsonResult.success("配置成功");
    }

    @RequestMapping("/ajax/menu")
    @ResponseBody
    public JsonResult ajaxMenuSetting(@RequestParam("roleId") Long roleId) {
        List<RoleMenu> roleMenuList = roleMenuService.getRoleMenuByRoleId(roleId);
        return JsonResult.success(roleMenuList);
    }

    @RequestMapping("/ajax/list")
    @ResponseBody
    public JsonResult ajaxList() {
        List<Role> roleList = roleService.getAllRole();
        return JsonResult.success(roleList);
    }

}
